package com.tourism.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.core.security.util.SecurityHolderUtils;
import com.tourism.domain.dto.attraction.AttractionQueryDTO;
import com.tourism.domain.entity.Attraction;
import com.tourism.domain.vo.AttractionVO;
import com.tourism.service.AttractionService;
import com.tourism.mapper.AttractionMapper;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 景点服务实现类
 *
 * @author lucky
 */
@Service
public class AttractionServiceImpl extends ServiceImpl<AttractionMapper, Attraction>
        implements AttractionService {

    @Override
    public Page<Attraction> getPage(Integer pageIndex, Integer pageSize, AttractionQueryDTO attractionQueryDTO) {
        String name = attractionQueryDTO.getName();
        String address = attractionQueryDTO.getAddress();
        Integer status = attractionQueryDTO.getStatus();

        LambdaQueryWrapper<Attraction> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(name), Attraction::getName, name);
        lqw.like(StrUtil.isNotBlank(address), Attraction::getAddress, address);
        lqw.eq(status != null, Attraction::getStatus, status);

        lqw.orderByDesc(Attraction::getUpdateTime);

        return page(Page.of(pageIndex, pageSize), lqw);

    }

    @Override
    public List<AttractionVO> getAllAttraction(AttractionQueryDTO attractionQueryDTO) {
        attractionQueryDTO.setUserId(SecurityHolderUtils.getUserId());
        return baseMapper.getAttractionList(attractionQueryDTO);
    }

    @Override
    public AttractionVO getAttractionById(Long id) {
        AttractionQueryDTO attractionQueryDTO = new AttractionQueryDTO();
        attractionQueryDTO.setId(id);
        attractionQueryDTO.setUserId(SecurityHolderUtils.getUserId());

        List<AttractionVO> attractionList = baseMapper.getAttractionList(attractionQueryDTO);

        if(!attractionList.isEmpty()){
            return attractionList.get(0);
        }

        return null;
    }
}




